#include <cstdint>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
    bool dfs(TreeNode*& node, int64_t max_num, int64_t min_num) {
        if (node == nullptr) {
            return true;
        }
        if (node->val <= min_num || node->val >= max_num) {
            return false;
        }
        return dfs(node->left, node->val, min_num) && dfs(node->right, max_num, node->val);
    }
public:
    bool isValidBST(TreeNode* root) {
        return dfs(root, INT64_MAX, INT64_MIN);
    }
};